package com.example.userdome.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.userdome.TokenUtil.JwtUtils;
import com.example.userdome.TokenUtil.decryptToken;
import com.example.userdome.TokenUtil.spiltUtils;
import com.example.userdome.mapper.ImagesMapper;
import com.example.userdome.mapper.UserMapper;
import com.example.userdome.pojo.Images;
import com.example.userdome.pojo.LoginRequest;
import com.example.userdome.pojo.Result;
import com.example.userdome.pojo.User;
import com.example.userdome.service.UserService;
import org.apache.poi.ss.formula.functions.T;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Date;

import static com.example.userdome.TokenUtil.HasLoginToken.hasLoginToken;

public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;

    @Resource
    private ImagesMapper imagesMapper;


    @Override
    public Result<T> userLogin(LoginRequest loginRequest, HttpServletResponse response, HttpServletRequest request) {
        // 检查是否已经有有效的登录Cookie
        if (hasLoginToken(request)) {
            return Result.success("你已经登录过了，请退出后重试！");
        }
        //判断用户名密码是否为空
        if (StringUtils.isEmpty(loginRequest.getUsername()) || StringUtils.isEmpty(loginRequest.getPassword())) {
            return Result.ACCOUNT();
        }

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", loginRequest.getUsername());
        //查询是否有此用户
        User login = userMapper.selectOne(queryWrapper);
        if (login == null) {
            return Result.failed();
        }
        //判断用户是否被禁用
        if (login.getStatus() == 1) {
            return Result.failed("你的账户被冻结，请联系管理员！");
        }
        //判断密码是否正确
        if (!login.getPassword().equals(loginRequest.getPassword())) {
            return Result.failed();
        }
        //组装token
        String token = JwtUtils.createToken(loginRequest.getPassword() + ":" + loginRequest.getUsername());
        // 将 token 存入 Cookie 中
        if (token != null) {
            Cookie cookie = new Cookie("token", token);
            cookie.setPath("/");
            cookie.setHttpOnly(true);
            cookie.setSecure(true); // 确保Cookie仅通过安全的HTTPS连接发送
            cookie.setMaxAge(60 * 60 * 24 * 7);
            response.addCookie(cookie);
            return Result.success("登录成功！");
        }
        return Result.failed("登录失败！");
    }

    @Override
    public Result<T> saveImage(Images images, String token, HttpServletResponse response, HttpServletRequest request) {
        String pe = decryptToken.decrypt(token);
        String userName = spiltUtils.getUserName(pe);
        images.setName(userName+"上传");
        images.setCreateTime(new Date());
        int insert = imagesMapper.insert(images);
        if (insert>0){
            return Result.success("上传成功！");
        }
        return Result.failed("上传失败！");
    }

    @Override
    public User getByUserName(String userName) {
        return lambdaQuery().eq(User::getUsername, userName).one();
    }
}
